package com.helper.dream.a361phonehelper.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pools;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.TuijianBibeiAdapter;
import com.helper.dream.a361phonehelper.bean.TuijianBibeiEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianBiBeiInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class TuijianBibeiFragment extends Fragment {
    View view;
    LayoutInflater inflater;
    List<TuijianBibeiEntity.DataBean> data = new ArrayList<>();
    TuijianBibeiAdapter adapter;
    StickyListHeadersListView lv;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tuijianbibei, container, false);
        this.inflater = inflater;
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter=new TuijianBibeiAdapter(getActivity(),data);
        lv= (StickyListHeadersListView) view.findViewById(R.id.lv_tj_bb);
        lv.setAdapter(adapter);
        getData();
    }

    private void getData() {
        Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ApiManager.TjBbBase).build();
        ITuijianBiBeiInterface iTuijianBiBeiInterface = retrofit.create(ITuijianBiBeiInterface.class);
        Call<TuijianBibeiEntity> call = iTuijianBiBeiInterface.getTjBb();
        call.enqueue(new Callback<TuijianBibeiEntity>() {
            @Override
            public void onResponse(Call<TuijianBibeiEntity> call, Response<TuijianBibeiEntity> response) {
                String temp = "";
                String cate;
                long ca=0;
                for (int i = 0; i < response.body().getData().size(); i++) {
                    cate = response.body().getData().get(i).getCategory_name();
                    if (!cate.equals(temp)) {
                        temp = cate;
                        ++ca;
                    }
                    response.body().getData().get(i).setTag(ca);
                }
                data.addAll(response.body().getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<TuijianBibeiEntity> call, Throwable t) {

            }
        });

    }
}
